import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/my-coupons',
      name: 'MyCoupons',
      component: () => import('@/views/MyCoupons.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/coupon-center',
      name: 'CouponCenter',
      component: () => import('../views/CouponCenter.vue')
    },
    {
      path: '/history',
      name: 'History',
      component: () => import('../views/History.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 